package cn.com.coding.app;

import cn.com.coding.common.lang.ResT;
import cn.com.coding.common.utils.PageUtil;
import cn.com.coding.system.domin.UserCardEntity;
import cn.com.coding.system.domin.dto.OrderGoodDto;
import cn.com.coding.system.domin.dto.UserCardProductDto;
import cn.com.coding.system.service.UserCardService;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;


/**
 * 订单表
 * 购物车这个概念你可以直接化为订单
 *
 * @author mxz
 */
@RestController
@RequestMapping("cat/userCard")
public class UserCardController extends BaseController {

    @Autowired
    private UserCardService userCardService;

    /**
     * 列表
     */
    @RequestMapping("/cardList")
    public ResT list(@RequestParam Map<String, Object> params) {
        PageUtil page = userCardService.queryPage(params);

        return ResT.ok().put("cardList", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{pid}")
    public ResT info(@PathVariable("pid") String pid) {
        UserCardEntity userCard = userCardService.getById(pid);

        return ResT.ok().put("userCard", userCard);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ResT save(@RequestBody UserCardEntity userCard) {
        userCardService.save(userCard);

        return ResT.ok();
    }

    /**
     * 加入购物车
     */
    @PostMapping("/addShoppingCart")
    public ResT addShoppingCart(@RequestBody Map<String, String> goodAdd) {

        String userPid = goodAdd.get("userPid");
        String productPid = goodAdd.get("productPid");

        if (userCardService.goodExist(userPid, productPid)) return ResT.ok(208, "该商品已在购物车，数量 +1");

        return ResT.ok().setData(MapUtil.builder().put("shoppingCartData", userCardService.saveGoodCard(userPid, productPid)).build());
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public ResT update(@RequestBody UserCardEntity userCard) {
        userCardService.updateById(userCard);

        return ResT.ok();
    }

    /**
     * 购物车支付
     */
    @RequestMapping("/addOrder")
    public ResT updateCard(@RequestBody OrderGoodDto orderGoodDto) {

        userCardService.addOrder(orderGoodDto);
        LocalDate date = LocalDate.now();

        AtomicReference<Integer> orderSum = new AtomicReference<>(0);
        orderGoodDto.getGoodList().stream().forEach(e -> {
            orderSum.updateAndGet(v -> v + (Integer.parseInt(e.getPrice().toString()) * e.getQuanity()));
        });
        redisUtil.hincr("mxz_user_order_sum", String.valueOf(date), orderSum.get());
        redisUtil.hincr("mxz_user_add_order", String.valueOf(date), 1);
        return ResT.ok();
    }

    /**
     * 用户获取订单
     */
    @PostMapping("/getOrder")
    public ResT getOrder(@RequestParam("userPid") String userPid) {

        List<UserCardEntity> cardList = userCardService.list(new QueryWrapper<UserCardEntity>().eq("user_pid", userPid).eq("card_status", 1).orderByDesc("update_time"));

        List<List<UserCardProductDto>> collect = cardList.stream().map(e -> userCardProductService.getOrderList(e.getPid())).collect(Collectors.toList());

        return ResT.ok().setData(MapUtil.builder().put("orders", collect).build());
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ResT delete(@RequestBody String[] pids) {
        userCardService.removeByIds(Arrays.asList(pids));

        return ResT.ok();
    }

}
